<?php
namespace app\common\model;

use think\Model;

/**
 * 合同-客户视图模型
 * Class AdminUser
 * @package app\common\model
 */
class ContractCustomerView extends Model
{
    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @param bool $need_array
     * @return array
     */
    public function getInfo(array $params, $fields = true, $need_array = false)
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
            $where['is_delete'] = $params['is_delete'];
        } else {
            $where['is_delete'] = 0;
        }

        if (!empty($where)) {
            $return = $this->where($where)->field($fields)->find();
            if (!empty($return) && $need_array) {
                $return = $return->toArray();
            }
        }

        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @param bool $is_need_arr
     * @return mixed
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true, $is_need_arr = false)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        /*合同编号*/
        if (isset($params['contract_number']) && !empty($params['contract_number'])) {
            if (is_array($params['contract_number'])) {
                $where['contract_number'] = ['in', $params['contract_number']];
            } else {
                $where['contract_number'] = ['like', '%' . $params['contract_number'] .'%'];
            }
        }

        /*合同状态*/
        if (isset($params['status'])) {
            if (is_array($params['status'])) {
                $where['status'] = ['in', $params['status']];
            } else {
                $where['status'] = $params['status'];
            }
        }

        /*合同年度*/
        if (isset($params['year']) && is_numeric($params['year'])) {
            $where['year'] = $params['year'];
        }

        /*项目名称*/
        if (isset($params['project_name']) && !empty($params['project_name'])) {
            $where['project_name'] = ['like', '%' . $params['project_name'] . '%'];
        }

        /*项目负责人名称*/
        if (isset($params['project_manager']) && !empty($params['project_manager'])) {
            $where['project_manager'] = ['like', '%' . $params['project_manager'] . '%'];
        }

        /*大类*/
        if (isset($params['class']) && is_numeric($params['class'])) {
            $where['class'] = $params['class'];
        }

        /*小类*/
        if (isset($params['type']) && is_numeric($params['type'])) {
            $where['type'] = $params['type'];
        }

        /*项目组id*/
        if (isset($params['project_group_id']) && $params['project_group_id'] > 0) {
            $where['project_group_id'] = $params['project_group_id'];
        }

        /*销售经理*/
        if (isset($params['sale_manager']) && !empty($params['sale_manager'])) {
            $where['sale_manager'] = ['like', '%' . $params['sale_manager'] . '%'];
        }

        /*客户名称*/
        if (!empty($params['customer_name'])) {
            $where['company_name|short_name'] = ['like', '%' . $params['customer_name'] . '%'];
        }

        /*合同签订公司code*/
        if (!empty($params['sign_company_code'])) {
            if (is_array($params['sign_company_code'])) {
                $where['sign_company_code'] = ['in', $params['sign_company_code']];
            } else {
                $where['sign_company_code'] = $params['sign_company_code'];
            }

        }

        /*客户编号*/
        if (!empty($params['customer_number'])) {
            $where['customer_number'] = ['like', '%' . $params['customer_number'] . '%'];
        }

        /*省*/
        if (isset($params['province_id']) && $params['province_id'] > 0) {
            $where['province_id'] = $params['province_id'];
        }

        /*市*/
        if (isset($params['city_id']) && $params['city_id'] > 0) {
            $where['city_id'] = $params['city_id'];
        }

        /*区*/
        if (isset($params['district_id']) && $params['district_id'] > 0) {
            $where['district_id'] = $params['district_id'];
        }

        /*星级*/
        if (isset($params['star_level']) && $params['star_level'] > 0) {
            $where['star_level'] = $params['star_level'];
        }

        /*金额区间*/
        if (isset($params['contract_amount_min']) && is_numeric($params['contract_amount_min']) &&
            isset($params['contract_amount_max']) && is_numeric($params['contract_amount_max'])
        ) {
            $where['contract_amount'] = ['between', [$params['contract_amount_min'], $params['contract_amount_max']]];
        }

        if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
            $where['is_delete'] = $params['is_delete'];
        } else {
            $where['is_delete'] = 0;
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit, false, ['query' => request()->param()]);

        return $is_need_arr ? $data->toArray() : $data;
    }
}